package meijuli.com.join51.business.utils;

import android.os.Build;
import android.text.TextUtils;

import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import com.jhx.common.utils.SystemConfigUtils;

import meijuli.com.join51.business.model.AppBean;

import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 * 检查更新
 *
 * @author Wizong 2017-05-19 13:09
 */
public class UpdateCheck {
    private OnUpdateCheckListener checkListener;
    private Call<HttpResponse<AppBean>> mCall;
    public static UpdateCheck getUpdateCheck() {
        return new UpdateCheck();
    }

    public UpdateCheck setUpdateCheckListener(OnUpdateCheckListener checkListener) {
        this.checkListener = checkListener;
        return this;
    }

    public void check(final boolean autoUpdate) {
        // 是否使用Bugly更新
        if (checkListener != null) {
            checkListener.onUpdateCheckStart();
        }

        // 第一个参数表示Android平台 ，第二个参数2表示商家版
        mCall = ApiManager.get().getApiService(ApiService.class).checkUpdate(1, 2,
                SystemConfigUtils.getVersionName(), SystemConfigUtils.getVersionCode(),
                Build.VERSION.SDK_INT);
        HttpRequest.getRequest().start(mCall, new ResponseCallback<AppBean>() {

            @Override
            public void onSuccess(String url, AppBean data) {
                if(data == null) {
                    return;
                }
                String currentVersion = SystemConfigUtils.getVersionName();
                checkUpdate(data, currentVersion, autoUpdate);
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                if (checkListener != null) {
                    checkListener.onUpdateCheckFail();
                }
            }
        });
    }

    public void check() {
        this.check(false);
    }


    private void checkUpdate(final AppBean appBean, final String currentVersion, boolean autoUpdate) {
        if (toVersionCode(currentVersion) < toVersionCode(appBean.getMainVersion())) {
            if (checkListener != null) {
                checkListener.onUpdateCheckComplete(appBean, currentVersion);
            }
        } else {
            if (autoUpdate) {
                checkListener.onUpdateCheckNew();
            }
        }
    }

    public static int toVersionCode(String versionName) {
        if (TextUtils.isEmpty(versionName)) {
            return 0;
        }

        try {
            return Integer.parseInt(versionName.replace(".", ""));
        } catch (NumberFormatException ignored) {
        }

        return 0;
    }

    public interface OnUpdateCheckListener {

        void onUpdateCheckStart();

        void onUpdateCheckComplete(AppBean appBean, String currentVersion);

        void onUpdateCheckNew();

        void onUpdateCheckFail();
    }
}
